﻿using Abp;
using Abp.Application.Navigation;
using Abp.Domain.Repositories;
using Abp.Localization;
using System;
using System.Collections.Generic;
using System.Text;

namespace WebApp.Navigations
{
    public class MyNavigationProvider : NavigationProvider
    {
        private readonly IRepository<UiMenu, Guid> _repository;
        public MyNavigationProvider(IRepository<UiMenu, Guid> repository)
        {
            _repository = repository;
        }

        public override void SetNavigation(INavigationProviderContext context)
        {
            var allMenus = _repository.GetAllList();
            foreach (var item in allMenus)
            {
                if (item.Pid != Guid.Empty)
                {
                    //子项
                    context.Manager.MainMenu.GetItemByName(allMenus.Find(p => p.Id == item.Pid).Name)
                     .AddItem(
                        new MenuItemDefinition(
                        item.Name,
                        new LocalizableString(item.DisplayName, AbpConsts.LocalizationSourceName),
                        url: item.Path,
                        icon: item.Icon,
                        customData: new { DisplayName = item.DisplayName, requiredPermissionName = item.PermissionKey }
                        )); 
                }
                else
                {
                    //父级菜单
                    context.Manager.MainMenu.AddItem(
                    new MenuItemDefinition(
                        item.Name,
                        new LocalizableString(item.DisplayName, AbpConsts.LocalizationSourceName),
                        url: item.Path,
                        icon: item.Icon,
                        customData: new { DisplayName = item.DisplayName, requiredPermissionName = item.PermissionKey }
                        ));
                
                }
            }
        }
        /**
        public override void SetNavigation(INavigationProviderContext context)
        {
            context.Manager.MainMenu
                .AddItem(
                    new MenuItemDefinition(
                        "TestCC",
                        new LocalizableString("Tasks", AbpConsts.LocalizationSourceName),
                        url: "/home/home1",
                        icon: "&#xe68a;",
                        customData: new { DisplayName = "Tasks", requiredPermissionName = "Tasks" }
                        )
                ).AddItem(
                    new MenuItemDefinition(
                        "main",
                        new LocalizableString("HomePage1", AbpConsts.LocalizationSourceName),
                        url: "/home/home",
                        icon: "&#xe68a;",
                          customData: new { DisplayName = "Tasks", requiredPermissionName = "Tasks1" }
                        )
                ).AddItem(
                    new MenuItemDefinition(
                        "setting",
                        new LocalizableString("Administration", AbpConsts.LocalizationSourceName),
                         url: "/setting",
                        icon: "&#xe68a;",
                          customData: new { DisplayName = "Tasks", requiredPermissionName = "Tasks1" }
                        ).AddItem(
                            new MenuItemDefinition(
                                "UiMenu",
                                new LocalizableString("UiMenu", AbpConsts.LocalizationSourceName),
                                url: "/setting/uiMenu/uiMenu",
                                icon: "&#xe68a;",
                                  customData: new { DisplayName = "UiMenu", requiredPermissionName = "Pages.UiMenu" }
                                )
                        ).AddItem(
                            new MenuItemDefinition(
                                "user",
                                new LocalizableString("user", AbpConsts.LocalizationSourceName),
                                url: "/setting/user/user",
                                icon: "&#xe68a;",

                                  customData: new { DisplayName = "Users", requiredPermissionName = "Pages.Users" }
                                )
                        ).AddItem(
                            new MenuItemDefinition(
                                "tenant",
                                new LocalizableString("Tenants", AbpConsts.LocalizationSourceName),
                                url: "/setting/tenant/tenant",
                                icon: "&#xe68a;",

                                  customData: new { DisplayName = "Tenants", requiredPermissionName = "SimpleTaskSystem.Permissions.Tenants" }
                                )
                        ).AddItem(
                            new MenuItemDefinition(
                                "role",
                                new LocalizableString("RoleManagement", AbpConsts.LocalizationSourceName),
                                url: "/setting/role/role",
                                icon: "&#xe68a;",
                                customData: new { DisplayName = "Roles", requiredPermissionName = "Pages.Roles" }
                                )
                        )
                ).AddItem(
                    new MenuItemDefinition(
                        "1home",
                        new LocalizableString("HomePage", AbpConsts.LocalizationSourceName),
                        url: "/Test/1",
                        icon: "&#xe68a;",

                          customData: new { DisplayName = "HomePage", requiredPermissionName = "Pages.Roles" }
                        )
                ).AddItem(
                    new MenuItemDefinition(
                        "2home",
                        new LocalizableString("2home", AbpConsts.LocalizationSourceName),
                        url: "/Test",
                        icon: "&#xe68a;",

                          customData: new { DisplayName = "2home", requiredPermissionName = "Tasks1" }
                        ).AddItem(
                                new MenuItemDefinition(
                                    "22home",
                                    new LocalizableString("22home", AbpConsts.LocalizationSourceName),
                                    url: "/Test/2",
                                    icon: "&#xe68a;",

                                      customData: new { DisplayName = "22home", requiredPermissionName = "Pages.Roles" }
                                    )
                            ).AddItem(
                                new MenuItemDefinition(
                                    "23home",
                                    new LocalizableString("23home", AbpConsts.LocalizationSourceName),
                                    url: "/Test/3",
                                    icon: "&#xe68a;",

                                      customData: new { DisplayName = "23home", requiredPermissionName = "Tasks1" }
                                    )
                            )
                            .AddItem(
                                new MenuItemDefinition(
                                    "3home",
                                    new LocalizableString("3home", AbpConsts.LocalizationSourceName),
                                    url: "/Test/Test",
                                    icon: "&#xe68a;",

                                      customData: new { DisplayName = "3home", requiredPermissionName = "Pages.Roles" }
                                    ).AddItem(
                                        new MenuItemDefinition(
                                            "31home",
                                            new LocalizableString("31home", AbpConsts.LocalizationSourceName),
                                            url: "/Test/Test/1",
                                            icon: "&#xe68a;",

                                              customData: new { DisplayName = "31home", requiredPermissionName = "Pages.Roles" }
                                            )
                                    ).AddItem(
                                        new MenuItemDefinition(
                                            "32home",
                                            new LocalizableString("32home", AbpConsts.LocalizationSourceName),
                                            url: "/Test/Test/2",
                                            icon: "&#xe68a;",

                                              customData: new { DisplayName = "Ta32homesks", requiredPermissionName = "Pages.Roles" }
                                            )
                                    )
                            )
                );
        }
        **/
    }
        
}
